<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>菜单管理</title>
  <meta http-equiv="Access-Control-Allow-Origin" content="*">
  <link rel="stylesheet" href="/layui/css/layui.css">
</head>
<body>
    <!-- 搜索栏 -->
    <div class="searchBar">
        名称：
        <div class="layui-inline">
          <input class="layui-input" name="name" id="name" autocomplete="off" />
        </div>
      <button class="layui-btn" id="searchBtn" data-Type="reload">搜索</button>
    </div>
    <!--表格-->
    <table id="user" lay-filter="user"></table>
    <!--工具栏-->
    <div id="toolbar" style="display: none">
        <div class="layui-btn-group">
          <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
        </div>
    </div>
    <!--操作栏-->
    <script type="text/html" id="operateBar">
      <a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete">删除</a>
      <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
      <a class="layui-btn layui-btn-sm" lay-event="reset">重置密码</a>
    </script>

    <!--模态框-->
    <div id="modal" style="display: none;">
        <form class="layui-form" lay-filter="entity">
            <input name="id" style="display: none;" />
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-inline">
                    <input type="text" name="username" required lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input">
                </div>
                <label class="layui-form-label">手机号</label>
                <div class="layui-input-inline">
                    <input type="text" name="phone" required lay-verify="required" placeholder="请输入手机号" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">邮箱</label>
                <div class="layui-input-inline">
                    <input type="text" name="email" required lay-verify="required" placeholder="请输入邮箱" autocomplete="off" class="layui-input">
                </div>
                <label class="layui-form-label">密码</label>
                <div class="layui-input-inline">
                    <input type="text" name="plainPassword" required lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">性别</label>
                <div class="layui-input-inline">
                    <input type="radio" name="sex" value="男" title="男" checked="">
                    <div class="layui-unselect layui-form-radio"><i class="layui-anim layui-icon"> </i><div>男</div></div>
                    <input type="radio" name="sex" value="女" title="女"><div class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim layui-icon"> </i><div>女</div></div>
                </div>
                <label class="layui-form-label">部门</label>
                <div class="layui-input-inline">
                    <select name="departmentId" id="departmentId">
                    </select>
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">职位</label>
                <div class="layui-input-inline">
                    <input type="text" name="grade" required lay-verify="required" placeholder="请输入职位" autocomplete="off" class="layui-input">
                </div>
                <label class="layui-form-label">待遇</label>
                <div class="layui-input-inline">
                    <input type="text" name="treatmentPrice" required lay-verify="required" placeholder="请输入待遇" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">地址</label>
                <div class="layui-input-inline">
                    <input type="text" name="address" required lay-verify="required" placeholder="请输入地址" autocomplete="off" class="layui-input">
                </div>
                <label class="layui-form-label">工作地址</label>
                <div class="layui-input-inline">
                    <input type="text" name="workAddress" required lay-verify="required" placeholder="请输入工作地址" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">就业时间：</label>
                <div class="layui-input-inline layui-col-md4">
                    <input type="text" class="layui-input" name="workDateTime" id="test2">
                </div>
                <label class="layui-form-label">就业状况：</label>
                <div class="layui-input-inline layui-col-md4">
                    <input type="radio" name="workStatus" value="1" title="工作" checked="">
                    <input type="radio" name="workStatus" value="2" title="休息">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">出生日期：</label>
                <div class="layui-input-inline layui-col-md4">
                    <input type="text" class="layui-input" name="workDateTime" id="test1">
                </div>
                <label class="layui-form-label">政治面貌：</label>
                <div class="layui-input-inline layui-col-md4">
                    <input type="text" name="politicalStatus" placeholder="政治面貌" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">可挂号数：</label>
                <div class="layui-input-inline layui-col-md4">
                    <input type="text" name="allowNum" lay-verify="number" placeholder="可挂号数" autocomplete="off" class="layui-input">
                </div>
                <label class="layui-form-label">已挂号数：</label>
                <div class="layui-input-inline layui-col-md4">
                    <input type="text" name="nowNum" lay-verify="number" placeholder="已挂号数" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">角色：</label>
                <div class="layui-input-inline">
                    <div id="role" name="role" class="demo-tree"></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-inline">
                    <button class="layui-btn" lay-submit="" lay-filter="addandupdate">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary form-reset">重置</button>
                </div>
            </div>
        </form>
    </div>
    <script src="/layui/layui.js"></script>
    <script>
        layui.use(['table','layer','jquery','form', 'tree'], function(){
        var table = layui.table;
        var layer = layui.layer;
        var $ = layui.jquery;
        var form = layui.form;
        var tree = layui.tree;

            $.get('/api/department',function (res){
                $("#departmentId").empty(); // 请求下拉框
                $("#departmentId").append(new Option("请选择", 0));
                $.each(res.data, function (i, obj) {
                    $('#departmentId').append(new Option(obj.name,obj.id));
                });
                form.render();
            });

            $.get('/api/role/getRole', function (res) {
                tree.render({
                    elem: '#role'
                    , id: 'roleId'
                    , data: res.data
                    , showCheckbox : true
                });
            });

            table.render({
            elem: '#user'
            ,id:'testReload'
            ,height: 525
            ,url: '/api/user/page' //数据接口
            ,page: true //开启分页
                , cols: [[
                {field: "id", title: "ID"},
                {field: "username", title: "姓名"},
                {field: "phone", title: "手机号"},
                {field: "email", title: "邮箱"},
                {field: "sex", title: "性别"},
                {field: "department", title: "科室"},
                {field: "address", title: "地址"},
                {field: "workAddress", title: "工作地址"},
                {field: "workDateTime", title: "就业时间"},
                {field: "workStatus", title: "状况", templet: function (d) {
                    return d.workStatus === '1' ? "工作" : "休息";
                  }
                },
                {field: "birthday", title: "出生日期"},
                {field: "politicalStatus", title: "政治面貌"},
                {field: "grade", title: "职位"},
                {field: "treatmentPrice", title: "待遇工资"},
                {
                  field: "createDatetime",
                  title: "创建时间"
                },
                {title: '操作', width: 200, toolbar: '#operateBar', align:
                          'center'}
              ]]
            ,toolbar:'#toolbar'
            ,response : {
              statusCode : 1
            }
            ,parseData : function (res) {
                if (res.status == 1) {
                    return {
                      'code': res.status,
                      'msg': res.message,
                    'count': res.total,
                            'data': res.data
                  }
                }
            }
        });

            table.on('toolbar(user)', function (obj){
                switch (obj.event) {
                    case 'add':
                        open2("新增用户",null);
                        break;
                }
            });
            table.on('tool(user)', function (obj){
                var data = obj.data;
                switch (obj.event) {
                    case 'reset':
                        break;
                    case 'delete':
                        layer.confirm("确定删除吗？", function (index) {
                            // 调用
                            $.ajax({
                                url : '/api/user/'+data.id,
                                type:'delete',
                                dataType:'json',
                                success: function (res) {
                                    if(res.status == 1) {
                                        layer.msg(res.message);
                                        obj.del();
                                    } else {
                                        layer.msg(res.message);
                                    }
                                }
                            });
                            // 关闭对话框
                            layer.close(index);
                        });
                        break;
                    case 'edit':
                        tree.reload('roleId',{});
                        $.get('/api/userRole/getUserRole?uid='+data.id,function (res) {
                            tree.setChecked('roleId', res.data);
                        });
                        open2('修改用户', data);
                        break;
                }
            });
            //"模态框",
            function open2(title, entity) {
                form.val("entity", entity);
                layer.open({
                    type: 1
                    , title: title
                    , content: $("#modal")
                    , offset: '10px'
                    , area: ['800px', '95%']
                    , end: function () {
                        $('.swiper-right-form-reset').click()
                        $('.form-reset').click();
                        table.reload('testReload');
                    }
                });
            }

            form.on('submit(addandupdate)', function (){
                // 取出表单中的数据项
                var data = form.val('entity');
                // 构建一个数组，用于存放用户选中的菜单id
                var roleIds = new Array();
                // 将用户的选择项获取到
                var checkedData = tree.getChecked('roleId');
                var index = 0;
                $.each(checkedData, function (i, obj) {
                    roleIds[index++] = obj.id;
                });
                data.roleIds = roleIds;
                var type = data.id ? 'put':'post';
                $.ajax({
                    url : '/api/user',
                    type : type,
                    dataType : 'json',
                    contentType : 'application/json',
                    data: JSON.stringify(data),
                    success: function (res) {
                    if (res.status == 1) {
                        layer.msg(res.message, {time:1*1000}, function () {
                            layer.closeAll();
                        });
                    } else {
                        layer.msg(res.message);
                    }
                }
            });
                return false;
            });

            var active= {reload : function(){
                    var name = $('#name');
                    // 重新加载表
                    table.reload('testReload', {
                        page:{
                            curr : 1
                        }
                        ,where : {
                            "username" : name.val()
                        }
                    })
                }
            };
            $('.searchBar .layui-btn').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        });
    </script>
</body>
</html>